package io.renren.modules.app.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.renren.common.utils.R;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.form.RegisterForm;
import io.renren.modules.app.service.UserService;
import io.renren.modules.quickService.constants.UserExtraEnum;
import io.renren.modules.quickService.entity.OrderEntity;
import io.renren.modules.quickService.entity.UserEntity;
import io.renren.modules.quickService.entity.UserExtraEntity;
import io.renren.modules.quickService.service.UserExtraService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 用户注册
 *
 * @author zenger380@163.com
 * @since 3.1.0 2018-09-25
 */
@RestController
@RequestMapping("/app/user")
@Api(value="APP用户基本信息相关接口",tags={"APP用户基本信息相关接口"})
public class AppUserController {
    @Autowired
    private UserExtraService userExtraService;
    @Autowired UserService appUserService;
    @Login
    @PostMapping("getCurrentUser")
    @ApiOperation("查询用户基本信息")
    public R getCurrentUser() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        Long userId = (Long)request.getAttribute("userId");
        UserEntity userEntity = appUserService.selectById(userId);
        UserExtraEntity userExtra = userExtraService.selectOne( new EntityWrapper<UserExtraEntity>().eq("user_id",userEntity.getUserId()));
        return R.ok().put("user", userExtra);
    }



    @Login
    @PostMapping("updateUserExtra")
    @ApiOperation("更新用户基本信息")
    public R updateUserExtra(@RequestBody RegisterForm form) {
        UserExtraEntity userExtra = new UserExtraEntity();
        userExtra.setExtraName(form.getExtraName());
        userExtra.setMale(form.getMale());
        userExtra.setId(form.getId());
        userExtra.setIdImageA(form.getIdImageA());
        userExtra.setIdImageB(form.getIdImageB());
        userExtra.setWechatId(form.getWechatId());
        userExtra.setBank(form.getBank());
        userExtra.setCardNum(form.getCardNum());
        userExtra.setAdminCode(form.getAdminCode());
        userExtra.setUpdateTime(new Date());
        userExtraService.updateById(userExtra);
        return R.ok().put("user", userExtra);
    }

}
